﻿using System;
using System.Configuration;
using System.Text;
using Navigant.Common;
using Navigant.Infrastructure.ClientPortals;
using Navigant.Infrastructure.Helpers;
using Navigant.Infrastructure.RepositoryFramework;

namespace Navigant.Infrastructure.UrlDictionary
{
    public static class UrlDictionaryService
    {
        private static IUrlDictionaryRepository repository;

        static UrlDictionaryService()
        {
            UrlDictionaryService.repository = 
                UrlDictionaryRepositoryFactory.GetUrlDictionaryRepository();
        }

        /// <summary>
        /// Takes a long url and creates a short version of it. Uses a custom dictionary to
        /// store the short version of the url as key and the original version as value.
        /// </summary>
        /// <param name="url">The original URL.</param>
        /// <returns>The shortened URL.</returns>
        public static string GetTinyUrl(string url, IClientPortal clientPortal)
        {
            Uri uri = new Uri(url);
            string key = UrlDictionaryService.repository.CreateEntry(url);
            StringBuilder urlBuilder = new StringBuilder();

            bool useSeparateAcknowledgmentSite = ConfigurationSettingsHelper.GetBoolean(clientPortal,
                Constants.UseSeparateAcknowledgmentSiteSetting);
            
            if (useSeparateAcknowledgmentSite)
            {
                string protocol = ConfigurationManager.AppSettings[Constants.ProtocolSetting];
                if (string.IsNullOrEmpty(protocol))
                {
                    protocol = Uri.UriSchemeHttps;
                }
                urlBuilder.Append(protocol);
                urlBuilder.Append("://");
                string host = ConfigurationManager.AppSettings[Constants.AcknowledgmentSiteHostNameSetting];
                if (string.IsNullOrEmpty(host))
                {
                    host = uri.Host;
                }
                urlBuilder.Append(host);
                string sitePortNumber = ConfigurationManager.AppSettings[Constants.AcknowledgmentSitePortNumberSetting];
                if (string.IsNullOrEmpty(sitePortNumber))
                {
                    sitePortNumber = Constants.DefaultAcknowledgmentSitePortNumber;
                }
                urlBuilder.AppendFormat(":{0}/", sitePortNumber);
            }
            else
            {
                urlBuilder.Append(uri.Scheme);
                urlBuilder.Append("://");
                urlBuilder.Append(uri.Host);
                urlBuilder.Append("/");
                urlBuilder.AppendFormat("{0}/", ConfigurationManager.AppSettings[Constants.AcknowledgmentVirtualDirectoryNameSetting]);
            }    

            urlBuilder.Append(ConfigurationManager.AppSettings[Constants.RedirectPageNameSetting]);
            urlBuilder.Append("?key=");
            urlBuilder.Append(key);

            return urlBuilder.ToString();
        }

        /// <summary>
        /// Takes a tiny url and retrieves the original version of it.
        /// </summary>
        /// <param name="url">The tiny URL value.</param>
        /// <returns>The original URL value.</returns>
        public static string GetFullUrl(string tinyUrl)
        {
            return UrlDictionaryService.repository.RetrieveValue(tinyUrl);
        }
    }
}
